When to (not) use a Custom Taxonomy
Saw a post over on the WordPress Stack Exchange site a while back, and though I sorta went into it in my answer there, I figured it would make a decent topic.
Custom taxonomies are great. But they’re not great for everything. When designing a custom solution, it’s helpful to understand this in advance, so you can choose what goes where properly.
What is a Taxonomy?
According to Wikipedia, Taxonomy is the practice and science of classification. I like this definition a lot, because it really points out what you’re doing with taxonomies. You’re classifying things. Or better yet, grouping them.
That word is important: Grouping. You’re putting things into groups. The taxonomy is the sum of those groups.
In the case of WordPress, you’re putting posts into groups. Maybe they’re custom post types of some kind (actually, they probably are if you’re doing it properly), but they still fit into the wp_posts table, so lets call them posts. You’re grouping posts together.
Categories group posts together. Tags group posts together. The way that these two are used is somewhat different, but that’s the basic end result here, right?
The group itself is the important bit. Not the term, the term you use for that group is just a label. It has no real significance whatsoever.
Now, I know that we often display the term, and I think that this is what confuses people. The category or tag is probably a word, in English (or your own language), and words have meaning. So yes, it does matter when I add “php” as a tag, or use the “Rants” category. But what’s important to realize is that that word only has meaning to people, not to the computers, and certainly not to our data design.
Enter Postmeta, Stage Left
Let’s say I’m building a new site, and I want to make, say, television shows be a custom post type. TV Shows are a pretty good choice for this example, as well as what the original SE question was about.
What makes sense as a taxonomy for a TV Show? Title? Actors? Episode numbers? Season numbers? For each item, you need to consider whether it makes more sense as a taxonomy or as pure post meta, or (rarely) both.
Titles makes perfect sense for a taxonomy. You’re grouping all the episodes of that show together, and people will want to see an episode listing. So yes, it’s a taxonomy.
Actors also makes sense as a taxonomy. Actors act in many roles, it would be nice to see what various shows and episodes they’ve been in.
Season and Episode numbers is another one. Every show has it’s own season and episode number. Usually this is represented as Season 1, Episode 1, sort of thing. At first glance, season numbers kinda makes sense as a taxonomy, since you can pull all of season 1 out. But on further reflection, no it doesn’t, because it doesn’t stand alone. Every TV series has a season 1. We don’t want to pull out all season 1 shows from all series, it’s not something we’ll need to do. Same goes for episode numbers, when are we going to make a query based on episode numbers, to get all the first episodes of all shows? Makes no sense. These should be postmeta (or “Custom Fields” as some people insist on calling them).
The difference is one of grouping. For Titles and Actors, we’re grouping individual things together in a meaningful way that stands alone. Sure, our labels (terms) will have meaning to us humans, but not to the act of the grouping itself. The group is a natural one. For seasons, the grouping is meaningful, but less so because it’s shared among non-similar things (different shows). It doesn’t stand alone. You could get around that by saying that your term would be title-season# or similar, but it really makes more sense as postmeta, since the season and episode number, taken together, are unique to the item we’re storing.
Postmeta as Unique Information
That’s the difference: Postmeta is bits of information that are specific to the post item itself. Taxonomies are bits of information shared, in a meaningful manner, by many different items. Title is shared across all episodes of a TV Show, and defines a meaningful relationship to all those episodes. Season number is shared, but it’s not meaningful because all shows have similar season numbers.
Notably, there’s easy ways to order by postmeta, but not by a taxonomy. Ordering by a taxonomy makes no real sense, because lots of items will share the same terms in that taxonomy. If I have 20 items all with “foo” as a term in a taxonomy, then what am I ordering them by? They’re all “foo” items.
As for querying, I can query based on either postmeta or taxonomy, if I really want to. Ideally, I’d do both. For example, if I wanted Firefly, Season 1, Episode 13, then I could specify both a tax_query for title and a meta_query for “S01E13” and get that one unique item. If I wanted all episodes of it in order, then I specify the tax_query for title and the meta query to select the season/episode metadata, then use the orderby to put them in the right order.
Choose Wisely
So if I can query by them both, but only order by postmeta, then what’s the difference? Why not use postmeta for everything?
Speed. Querying for a taxonomy is loads faster than querying for posts with certain meta information. Even better, I can use both to make things even speedier. The meta_query for S01E13 is going to be loads faster when I specify that title taxonomy, because now it’s only looking for S01E13 amongst Firefly episodes, not amongst all episodes of all shows.
The bottom line is that it’s best to use a taxonomy for attributes that a) define a natural grouping of your items, and b) which are natural labels and not inherently useful data to your methods. The title is not inherently a useful data (it’s just text, could be anything), but the episode number is a number which you will want to order by and display/change/set. You use that episode number for doing something.
Also, it’s perfectly understandable for individuals to disagree on any given example. There is no “right” answer, there’s only the answer that satisfies your own personal use cases. When designing your taxonomies, just remember to think about grouping of items and how you’re going to use the data you’re defining. If it’s a group in how you are going to use it, then a taxonomy works better than postmeta. But if you need the attribute to be manipulated in some manner, then a postmeta works better.
Hi Otto,
thanks for the great post – this has cleared up a fair bit of my confusion about taxonomies.
It also made me rethink my approach about a specific case though, and I’d love to hear your opinion on it:
I’m working on a website for a community radio station and am trying to create a list of shows and a schedule.
My current approach is a custom post type (CPT) “shows”, a CPT “schedule”, a taxonomy (TAX) “frequency” and a hierarchical TAX “genre”. So a show could be classified as “weekly” and “music”->”world”,”jazz”,”funk”.
A schedule entry would require to choose the show name and contain postmeta for start and end time in addition to a description field for the show’s “episode”. In order to choose the show name I was intending to use Scribu’s Posts2Posts plugin.
Your post made me think whether I should rather use a show TAX to group schedule entries together (but then that’d either mean creating a TAX and a CPT for every show, or maybe using something like http://www.deluxeblogtips.com/p/taxonomy-meta-script-for-wordpress.html (scrapping the “shows” CPT altogether).
What would be your approach in a case like this?
Thanks,
Axel
You appear to be describing a database system, as opposed to a blog.
Is this dBase IV we’re talking about, or WordPress?
You are aware that WordPress runs on a database, right?
Well, main problem is people reaching taxonomies more easily then custom meta boxes etc, you know. When we have a core meta box system like CCK or something, people will use it rather then taxonomy imo.
Thanks.
I’m the guy that asked the question 🙂 Thank you for your help and advice.
Enjoy the beer 😉
Thank you so much for this post Otto, it’s just the thing I’ve been looking for regarding Taxonomies vs Postmeta.
Here’s an idea I’ve got for being able to order by a taxonomy:
Can you have a taxonomy term ALSO entered in as postmeta? I’m sure there’s probably some way to automate that, but taking from your TV shows example, yes every TV show has an Episode 1. So it makes sense to keep that as postmeta for ordering the episodes chronologically per each season.
However I could see cases where maybe you also want to query every Episode 1 from Season 1 of each TV show. Maybe somebody wants to check out all the pilot episodes… that doesn’t seem too out of the question to me. You’d then want to have a taxonomy for episode number in that case for easy querying.
Of course I guess this would only work with a taxonomy where you select/enter only 1 term. Otherwise I don’t think it would make sense.
Another example is I’m working on a Pokemon card database site. One piece of data I’m working with is the card name. I’d like to allow the user to sort cards alphabetically, so in that case it’s good to have the card name be postmeta.
But I can also see instances where a user might want to view all the Pikachu cards ever made (so card name = Pikachu). It would be easier to query if I ALSO had a Card Name taxonomy (non-hierarchal, only 1 term added for each post).
So I think there are a lot of cases where it could be good to have both postmeta and taxonomy term for certain items… what do you think? Any disadvantage to doing that rather than making complicated query/sorting functions?
Hey Otto, quick thanks for a clear breakdown of this topic. Really made it understandable. I would buy you a beer but alas, at the time of this writing, it is now an empty glass.
MG
Thanks Otto. Very helpful.
I’m working on geo websites at the moment that each cover one UK county. I’m using a place taxonomy structure to group villages round major towns to form local mini sites within each county. Meanwhile I’m using categories to describe types of entity (eg attractions, types of business), so giving two dimensions to the data.
Once again, thanks…
Indeed great article and agree taxonomies are a great way to manage everything in the WordPress. While there are more to improve for recently implemented taxonomies (since 3.0) here is a great tool to help you order all terms for a given taxonomy, either by custom drag and drop or automatically by title, date, id, count, random etc Advanced Taxonomy Terms Order some of you might find this very useful as it already helped me allot.
Great article. Have been trying to get an understanding of the relationship between taxonomies and custom posts – very helpful
i think i have been looking for exactly this. i have a taxonomy that i’ve limited to only 3 terms and have created my own tax metabox to make sure that a post can only ever have 1 value: featured, normal or excluded. it struck me that this is sort of post_meta territory but if querying by tax is significantly faster i think i will leave it as is.
otto, could you answer my question at stackexchange?
http://wordpress.stackexchange.com/questions/52998/how-to-restric-term-input-under-one-parent-or-a-term-group
Hi Otto, I’m coming back to your post months later. 🙂 i still think there is a valid reason to sort by taxonomy. What if you have a post type “Articles” with a “subject” taxonomy and you wanted to show all the articles grouped by subject
Apple Subject
apple article 1
apple article 2
Bacon Subject
bacon article 1
bacon article 2
and so on. the subject taxonomy is still a ‘grouping’ of articles, where the terms are shared across more than one article. do you think this is possible with enough magic thrown at the posts_join and posts_orderby filters?
thanks for this post. in summary – taxonomy = groupings vs postmeta = unique
iam confused what is the different now between custom fields and tags???
thanks for the post. your approach is really clear and helpful
Thanks for that Otto.
So it can come down to the basic idea: CF = manipulate the data and CT = querying/listing the data.
Let’s just say I need to use the data in both ways (manipulation AND querying)… what would you recommend if you HAD to choose ONE method. CFs or CTs?
Cheers, Pete
Thanks very much for taking the time out of your day to blog about this. It’s a very complicated area and while I’m still confused, this has helped tremendously. Cheers!
There are area where ordering by taxonomies makes sense, for example when you want to order by posts which have the highest number of shared taxonomies, tags for example. Posts which have an higher number of shared tags will probably be more related than posts with less shared tags, in this case post meta is not gonna be any more useful than taxonomies since it can only order by alpha or num and you will end up with the same amount of custom code doing a custom ordering either be it with post meta or taxonomies.
I was trying my best to organize posts based on categories, which I found little difficult. Post meta is really a good solution, which does give capabilities like CCK fields in Drupal. This do help us to create content on WordPress when the content becomes complicated. Thanks for your great article.
Dear otto,
Thank you for your article. But after 4 years it is little out dated.
Could you please make some comments for http://stackoverflow.com/questions/30614345/perfomance-comparison-between-querying-by-taxonomy-vs-querying-by-custom-fields
Warmest Regards
Deniz
[…] like Custom Fields vs. Taxonomies – Alex King state, When to (not) use a Custom Taxonomy – Otto that custom fields should not be used for filtering, because those values are not […]
I’d love to see some(any) data(quantitative empirical evidence) to support this. There’s a lot of theory around but next to nothing when to comes to benchmark testing for a given scenario.
Your question cannot be answered because you don’t give a proper scenario or framework by which to answer it.
Searching by meta value only is inefficient because it is not an indexed value. Searching by key and value is not as bad, because the key is indexed. Still bad, but not as bad for small sets. For it to be noticably bad, you would need to have multiple tens of thousands of posts and millions of meta. A large news site, for example.
If you’re talking about a rather small blog, then do what you like. Everything depends on scale.
No quantitative data exists because the answer is self-evident to anybody who understands databases and basic SQL queries. There is no need to measure that which is obvious.
Here’s the thing… I can’t give you a framework because I don’t know when what circumstances might make searching “inefficient”. I *could* give you a “framework” and have it tested only to find out it to be fine for CF searching, but then I’d just keep on giving you more frameworks until the CF searching became “inefficient”.
What I’d like to see is the people who keep on telling me “Searching by meta value only is inefficient” to design a standardized framework to test it.
Yes, yes I know there are sooo many variables but that’s what the experts can nut out. Start with *a* framework and fork variations off from that.
Everything needs a baseline to build from but I’m not the guy to decide what that should be. I’m just the annoying guy who doesn’t accept “There is no need to measure that which is obvious.” and throws it back on you and says “OK, show me how obvious it is”.
“For it to be noticeably bad, you would need to have multiple tens of thousands of posts and millions of meta”. That statement is getting a little bit close to something
quantitative.
OK, based on your statement above and for argument’s sake this is “Pete’s Custom Field Vs Custom Taxonomy Test Framework” (but I have no idea what variables would best be measured? speed? no. of queries? memory used? )
100k posts 10,000k meta 10,000k tax
10k posts 10,000k meta 10,000k tax
1k posts 10,000k meta 10,000k tax
100k posts 1000k meta 10,000k tax
10k posts 1000k meta 10,000k tax
1k posts 1000k meta 10,000k tax
100k posts 100k meta 10,000k tax
10k posts 100k meta 10,000k tax
1k posts 100k meta 10,000k tax
100k posts 10,000k meta 1000k tax
10k posts 10,000k meta 1000k tax
1k posts 10,000k meta 1000k tax
100k posts 1000k meta 1000k tax
10k posts 1000k meta 1000k tax
1k posts 1000k meta 1000k tax
100k posts 100k meta 1000k tax
10k posts 100k meta 1000k tax
1k posts 100k meta 1000k tax
100k posts 10,000k meta 100k tax
10k posts 10,000k meta 100k tax
1k posts 10,000k meta 100k tax
100k posts 1000k meta 100k tax
10k posts 1000k meta 100k tax
1k posts 1000k meta 100k tax
100k posts 100k meta 100k tax
10k posts 100k meta 100k tax
1k posts 100k meta 100k tax
That’s the problem though. You want the answer, and I already know the answer. I don’t need to test it, you do. I don’t need to design anything in order to convince you, because I have no need to convince you. You can either accept the answers I know to be correct, or not. The only person hurt by not accepting them is you, not me.
Basically, you’re asking me (or anybody else) to do a bunch of work that does not benefit me in any way. Sorry, but I’m just not that interested.
We have different ideas of an “answer”. I want numbers, you want to give me words.
I don’t have any numbers to give you, and I have no need, nor desire, to spend a bunch of time to create numbers to prove an answer that is glaringly obvious. I have better things to do with my time.
“Basically, you’re asking me (or anybody else) to do a bunch of work that does not benefit me in any way. Sorry, but I’m just not that interested”
I’m asking anyone who’ll listen, not just you. Thanks for listening.
What if we’re talking about a magazine with issues and volumes.
A volume is a unique grouping for a set of issues by year.
Issue numbers on the other hand are repeated every volume/year.
By your explained tax logic the volume should be a taxonomy and the issue number post meta?
Otto, I have so much respect for your knowledge, especially when it comes to WordPress. I have learned much from you over the years. I also have benefited from your writing on teh architecture of WordPress.
This is why I was excited to see your answer here: http://wordpress.stackexchange.com/questions/14306/using-wp-query-is-it-possible-to-orderby-taxonomy/14309#14309 when I was searching the topic.
I came to this article on your site because this has left me with one question, what about WooCommerce attributes? This is a very widely used instance where a taxonomy is used for relevant data. It is especially useful when a store has thousands of products that can be sorted by their relevant data. Your point is still relevant. Because most of the cases you would not seed to order items by that data just group them. But there are many WooCommerce stores using taxonomies for important data, this must cause some need for orderby tax, no?
Great post thank you! I’ve been wondering if it was wrong to use multiple category taxonomies.
Awesome Tutorial. I was using custom fields for mobile specs which i had converted to taxonomies. It helps me a lot. Thanks
Hey everyone!
I’m trying to implement what Otto posted here, but I don’t get it completely.
I have a magazine website where I need to show all articles they ever published. Therefor I came up with the following data structure.
Post Type: Issue article
Taxonomy: Article author – So I can query all articles published by one author in this magazine
Taxonomy: Volume – So I can query all articles in one volume (some volumes have 2 years span)
Taxonomy: Article topic – So I can query all articles having the same topic within all volumes and issues
Post-Meta: Issue (1, 2, 3 or 4) – Radio button in meta box
Post-Meta: Page number – text field
I try to list all articles in one volume in one taxonomy archive grouped by there topic. -> WP_Query doesn’t do it.
Q:
What is wrong in my plan? Should the topic be post meta?
If yes, why?
The topic is not a sub-thing to any of the other taxonomies to me.
PS: The articles for one topic are not one behind the other. So I can’t use the post meta and store the topic split somewhere.
Happy for any explanation the solve the logic in this case.
Greetings
derRALF
I go by the following rule… If you can imagine that you’d have a search function, what features would you allow your users to browse/search your posts by? If the data can be searchable make it a taxonomy, if it’s just useful info but not something to be searched then make it post meta.
if you want to query the article post and group it with other posts with the same article topic then I would make it a taxonomy.
Query = taxonomy
Useful info but not queried = post meta
@Pete: From reading your comment, my taxonomies and meta data are structured the right way.
But still I can’t sort/group my posts in one taxonomies by/with an other taxonomy.
[…] So, the main difference between custom fields and taxonomies here is grouping. Samuel Wood (also known as Otto) clarifies this difference in a very well-written post. […]